package com.matrix.tool.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.Typeface
import android.os.Handler
import android.os.Message
import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.blankj.utilcode.util.ScreenUtils

class VerticalTextView  @JvmOverloads constructor(context: Context,
                       attrs: AttributeSet? = null,
                       defStyleAttr: Int = 0) :
    View(context,attrs,defStyleAttr) {

    private var mPaint: Paint? = null
    private var width = 300
    private var color = Color.BLUE
    private var text = "发射爱心❤弹幕"
    private var mRect: Rect? = null
    var mContext: Context = context
    private var mScreenWidth = 0
    private var mScreenHeight = 0
    private var vTextViewLength = 0
    private var movePos = 0
    private var speed = 20
    private var font:Typeface? = null
    private val textWidth = 20
    private val mHandler: Handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                1 -> {
                    movePos = if (movePos < -vTextViewLength) {
                        mScreenHeight
                    } else {
                        movePos - speed
                    }
                    invalidate()
                    sendEmptyMessageDelayed(1, 40)
                }
            }
        }
    }

    fun setSpeed(speed: Int) {
        this.speed = speed
    }

    fun setFont(font: Typeface) {
        this.font = font
        invalidate()
    }

    fun startAnimation() {
        mHandler.sendEmptyMessage(1)
    }

    fun stopAnimation() {
        if (mHandler != null) {
            mHandler.removeMessages(1)
        }
    }


    fun setWidth(width: Int) {
        this.width = width * 10
        invalidate()
    }

    fun getvTextViewLength(): Int {
        return vTextViewLength
    }

    fun setColor(color: Int) {
        this.color = color
        invalidate()
    }

    fun setText(text: String) {
        this.text = text
        invalidate()
    }

    init {
        init(context, null, 0)
    }

    private fun init(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
        this.mContext = context
        mScreenWidth = ScreenUtils.getScreenWidth()
        mScreenHeight = ScreenUtils.getScreenHeight()
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mPaint!!.isAntiAlias = true
        mPaint!!.style = Paint.Style.FILL
        mRect = Rect()
        mPaint!!.textSize = width.toFloat()
        mPaint!!.getTextBounds(text, movePos, text.length, mRect)
        vTextViewLength = mRect!!.width()
        movePos = mScreenHeight
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        mPaint!!.strokeWidth = textWidth.toFloat()
        mPaint!!.color = color
        mPaint!!.textSize = width.toFloat()
//        mPaint!!.setTypeface(font)
        val rect = Rect()
        mPaint!!.getTextBounds(text, 0, text.length, rect)
        val h = rect.height()
        vTextViewLength = rect.width()
        val fontMetrics = mPaint!!.fontMetrics
        val distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom
        //        float baseline=/*distance+*/h;
        val matrix = Matrix()
        matrix.setRotate(90f, 0f, 0f)
        canvas.setMatrix(matrix)
        canvas.drawText(text, movePos.toFloat(), (-mScreenWidth / 2 + h / 3).toFloat(), mPaint!!)
    }

}